<script>
	import pushInit from "./utils/push.js";
	import store from "./store/index.js";
	import {
		apiSubmitDeviceClientId
	} from "./api/device_api.js";
	import i18 from "./lang/index.js"
	import {
		i18nTool
	} from './utils/i18en.js';
	import {
		gesturePwdState,
		getGesturePwd
	} from "./utils/user_tools.js";

	export default {
		onLaunch: async function() { 
			console.log('App Launch')
			let systemInfo = uni.getSystemInfoSync();
			// pushInit();
			if (systemInfo.language.includes('zh')) {
				i18.global.locale.value = 'zh'
				store.commit('setLanguage', 'zh');
			} else {
				i18.global.locale.value = 'en'
				store.commit('setLanguage', 'en');
			}
			this.setTabbarItem();

			this.getClientInfoLoop();
			if (!uni.getStorageSync("token")) {
				await uni.reLaunch({
					url: "/pages/login/login",
					success() {

					}
				})
			} else {
				if (getGesturePwd() && gesturePwdState()) {
					await uni.reLaunch({
						url: "/pages/login/gesturePwd"
					})
				}
			}

		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			async submitClientId(cid) {
				if (uni.getStorageSync("token")) {
					let res = await apiSubmitDeviceClientId({
						cid: cid
					})
				}
			},
			// 循环获取clientid信息,直到获取到为止
			getClientInfoLoop() {
				// #ifdef APP
				var timer = setInterval(() => {
					plus.push.getClientInfoAsync((info) => {
						clearInterval(timer);
						let cid = info["clientid"];
						this.submitClientId(cid);
					});
				}, 1000);

				//显示本地通知
				plus.push.addEventListener("receive", function(msg) {
					plus.push.createMessage(msg.content, msg.payload, {
						title: msg.title,
					});
				})
				//点击事件
				plus.push.addEventListener("click", (payload) => {
					uni.reLaunch({
						url: "/pages/message/message"
					})
				})
				// #endif

			},

			setTabbarItem() {
				uni.setTabBarItem({
					index: 0,
					text: i18nTool('tabbarItem.home'),
				})
				uni.setTabBarItem({
					index: 1,
					text: i18nTool('tabbarItem.track'),
				})
				uni.setTabBarItem({
					index: 2,
					text: i18nTool('tabbarItem.message'),
				})
				uni.setTabBarItem({
					index: 3,
					text: i18nTool('tabbarItem.function'),
				})
				uni.setTabBarItem({
					index: 4,
					text: i18nTool('tabbarItem.my'),
				})
			}
		}
	}
</script>

<style>

</style>